<?php
namespace Home\Event;

use Home\Dto;
use Home\Dto\BaseDto;

class StargameThirdEvent extends ThirdAbstractEvent
{
    public function share(BaseDto $dto) {
        if (!$dto->id || !$dto->uid) {
            $this->error = '参数错误';
            return false;
        }

        $user = service('sso')->where(['uid' => $dto->uid, 'channel' => 'stargame'])->find();
        $video = event('video')->info(intval($dto->id));

        if (empty($user) || empty($video)) {
            $this->error = '参数错误';
            return false;
        }

        $nga_domain = 'http://bbs.ngacn.cc';

        $url = $nga_domain.'/post.php';
        $content = $video['detail'];
        $content .= '[flash]'.U('share/video', ['id' => $video['id']], false, true).'[/flash]';
        $params = [
            'fid' => 528,
            'step' => 2,
            'lite' => 'js',
            'post_subject' => '[66PLAY]'.iconv('UTF-8', 'GBK', $video['title']),
            'post_content' => $content,
            'ngaPassportUid' => 'stargametoken0'.$user['open']['userId'],
            'ngaPassportCid' => $user['open']['access_token'],
        ];

        $header = [
            "content-type: application/x-www-form-urlencoded; charset=GBK"
        ];
        $response = http_request($url, $params, $header, 'post');

        $result = iconv('GBK', 'UTF-8', $response);

        $result_json = str_replace('window.script_muti_get_var_store=', '', $result);

        $res = json_decode($result_json, true);

        if (!isset($res['data']['__MESSAGE'][4]) || empty($res['data']['__MESSAGE'][4])) {
            $this->error = $res['data']['__MESSAGE'][1];
            return false;
        }
        return $nga_domain . $res['data']['__MESSAGE'][4];
    }
}